package com.wenhui.schoolyardinfo.square.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.callback.RcvOnItemViewClickListener;
import com.wenhui.schoolyardinfo.customview.CircleImageView;
import com.wenhui.schoolyardinfo.square.bean.RecommendBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/7/5 09:19
 * introduce : 为你推荐Adapter
 */

public class RecommendAdapter extends RecyclerView.Adapter {

    private static final String TAG = "RecommendAdapter";
    private Context context;
    private List<RecommendBean> recommendBeanList = new ArrayList<>();

    public RecommendAdapter(Context context) {
        this.context = context;
    }

    public RecommendAdapter(Context context, List<RecommendBean> recommendBeanList) {
        this.context = context;
        this.recommendBeanList = recommendBeanList;
    }

    public void clearData() {
        recommendBeanList.clear();
        notifyDataSetChanged();
    }

    public void addAllData(List<RecommendBean> recommendBeanList) {
        this.recommendBeanList.addAll(recommendBeanList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recommend_content, null);
        return new ContentHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ContentHolder contentHolder = (ContentHolder) holder;
        Glide.with(context).load(recommendBeanList.get(position).getHeadPortraitUrl()).into(contentHolder.imvHeadPortrait);
        contentHolder.tevName.setText(recommendBeanList.get(position).getName());
        contentHolder.tevDynamic.setText(recommendBeanList.get(position).getDynamicNum() + "条动态 ");
        contentHolder.tevFans.setText(recommendBeanList.get(position).getFansNum() + "粉丝");
    }

    @Override
    public int getItemCount() {
        return recommendBeanList.size();
    }

    protected class ContentHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.imv_head_portrait)
        CircleImageView imvHeadPortrait;
        @BindView(R.id.tev_name)
        TextView tevName;
        @BindView(R.id.tev_dynamic)
        TextView tevDynamic;
        @BindView(R.id.tev_fans)
        TextView tevFans;
        @BindView(R.id.tev_concern_add)
        TextView tevConcernAdd;
        @BindView(R.id.layout_concern_add)
        RelativeLayout layoutConcernAdd;
        @BindView(R.id.layout_recommend_for_you)
        RelativeLayout layoutRecommendForYou;
        @BindView(R.id.view_recommended_for_you)
        View viewRecommendedForYou;

        public ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            layoutConcernAdd.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layout_concern_add:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    private RcvOnItemViewClickListener rcvOnItemViewClickListener;

    public void setRcvOnItemViewClickListener(RcvOnItemViewClickListener rcvOnItemViewClickListener) {
        this.rcvOnItemViewClickListener = rcvOnItemViewClickListener;
    }
}
